#include"RpcConfig.h"

#include<iostream>
#include<string>
void RpcConfig::Trim( std::string & str)
{
    int index = str.find_first_not_of(" ");
     if(-1 != index)
     {
       str = str.substr(index,str.size()-index);
     }
    
      index = str.find_last_not_of(" ");
     if(-1 != index)
     {
        str=str.substr(0,index+1);
     }
}
void RpcConfig:: LoadConfigFile(const char * config_file)
{
    if(nullptr == config_file)
    {
        return ;
    }
    FILE * fp = fopen(config_file,"r");
    if(nullptr == fp)
    {
        std::cout<<fp<<"is not exist"<<std::endl;
        return ;
    }
    while(!feof(fp))
    {
        char buff[512];
        fgets(buff,512,fp);

    
      std::string tBuff(buff);
      Trim(tBuff);
     if(tBuff[0] == '#' || tBuff.empty())
     {
        continue;
     }

     int index = tBuff.find("=");
     int rIndex=-1;
     if(-1 == index)
     {
        std::cout<<"configfile error"<<std::endl;
     }
     std::string key,value;
     key = tBuff.substr(0,index);
     rIndex = tBuff.find('\n',index);
     value =tBuff.substr(index+1,rIndex-index-1);
     m_configMap.insert(std::make_pair(key,value));
    }
}
std::string RpcConfig::Load(const std::string key)
{
    auto it = m_configMap.find(key);
    if(it == m_configMap.end())
    {
       return std::string("");
    
    }
    return it->second;

}